package main.java.tmi.productAdjust.web;

import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.service.UserVO;
import main.java.tmi.common.util.AjaxPageSplit;
import main.java.tmi.productAdjust.service.ProductAdjustService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class ProductAdjustController {
	
    @Resource(name = "productAdjustServiceImpl")
    private ProductAdjustService productAdjustService;
    
    /**
     * The Constructor
     */
    public ProductAdjustController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productAdjust")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
    		ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productAdjust start=========================");
        System.out.println("====================================================");
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
		model.addAttribute("roleId",userInfo.getRoleId());
        return "/tmi/productAdjust/productAdjustList";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productAdjustDetail")
    public String viewDetail(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productAdjustDetail start=========================");
        System.out.println("====================================================");
        model.addAttribute("adjustNo",param.get("adjustNo"));
        return "/tmi/productAdjust/productAdjustDetail";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productAdjustWrite")
    public String viewWrite(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productAdjustWrite start=========================");
        System.out.println("====================================================");

        return "/tmi/productAdjust/productAdjustWrite";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productAdjustUpdate")
    public String viewUpdate(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productAdjustUpdate start=========================");
        System.out.println("====================================================");
        System.out.println("==========================param=========================="+param);
        model.addAttribute("adjustNo",param.get("adjustNo"));
        return "/tmi/productAdjust/productAdjustUpdate";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getProductAdjustView")
    public void getProductAdjustView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getProductAdjustView start");
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int productAdjustCount = productAdjustService.getProductAdjustViewCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(productAdjustCount, pagingTmp, listScale, 5, "");
		System.out.println("===========pageSplit============> "+pageSplit);
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("productAdjustCount",productAdjustCount);
		model.addAttribute("resultList",productAdjustService.getProductAdjustView(param));
    }
    
    /**
     * Detail search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getProductAdjustDetailView")
    public void getProductAdjustDetailView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getProductAdjustDetailView start");
        System.out.println("getProductAdjustDetailView start=======================>"+param);
        
        //조회
		model.addAttribute("resultDetailMap",productAdjustService.getProductAdjustDetailView(param));
    }
    
    /**
     * order SAVE
     */
    @RequestMapping("/saveProductAdjust")
    public void saveProductAdjust(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> saveProductAdjust Start");
    	boolean resultData = true;
    	String saveStatus = param.get("saveStatus").toString();
    	
    	if(saveStatus.equals("SAVE")){
    		String dupChk = productAdjustService.productAdjustDupChk(param);
    		if(dupChk.equals("Y")){
        		resultData = productAdjustService.addProductAdjust(param);
    		}else{
    			model.addAttribute("resultDup",dupChk);
    		}
    	}else if(saveStatus.equals("UPDATE")){
    		resultData = productAdjustService.addProductAdjust(param);
    	}
        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
    
    /**
     * order de
     */
    @RequestMapping("/delProductAdjust")
    public void delProductAdjust(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> delProductAdjust Start");
    	
    	boolean resultData = productAdjustService.delProductAdjust(param);

        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
}
